import { fileURLToPath, URL } from "url";
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import styleImport, { VantResolve } from "vite-plugin-style-import";
import path from 'path'
import {createSvgIconsPlugin} from 'vite-plugin-svg-icons'
const Timestamp = new Date().getTime();

// https://github.com/vuetifyjs/vuetify-loader/tree/next/packages/vite-plugin
import vuetify from 'vite-plugin-vuetify'

// https://vitejs.dev/config/
export default defineConfig({
  publicPath: "./",
  base: "./",
  // 反向代理
  server: {
    port: 8010,
    // 是否自动在浏览器打开
    open: false,
    // 是否开启 https
    https: false,
    proxy: {
      "/api": {
        target: "http://test.skalibrary.com.cn/",
        // target: "https://sh.swechat.cc/",
        // target:"http://192.168.121.111/",

        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, ""),
      },
    },
  },
  plugins: [
    vue(),
    vuetify({
      autoImport: true,
    }),
    styleImport({
      resolves: [VantResolve()],
      libs: [
        {
          libraryName: "vant",
          esModule: true,
          resolveStyle: (name) => `../es/${name}/style`,
        },
      ],
    }),
    createSvgIconsPlugin({
      // 指定要缓存的文件夹
      iconDirs: [path.resolve(process.cwd(), 'src/assets/svg')],
      // 指定symbolId格式
      symbolId: '[name]'
    }),
  ],
  resolve: {
    alias: {
      "@": fileURLToPath(new URL("./src", import.meta.url)),
    },
  },
  build: {
    cssCodeSplit: false,
    rollupOptions: {
      output: {
        entryFileNames: `assets/[name].${Timestamp}.js`,
        chunkFileNames: `assets/[name].${Timestamp}.js`,
        assetFileNames: `assets/[name].${Timestamp}.[ext]`,
      },
    },
  },
});
